home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / gnu / amiga / bmake15.lzh / param.c < prev    next >
C/C++ Source or Header  |  1991-11-02  |  4KB  |  204 lines

  1. /*    param.c
  2.  *    (c) Copyright 1991 by Ben Eng, All Rights Reserved
  3.  *
  4.  */
  5.  
  6. #include <ctype.h>
  7. #include <scdir.h>
  8.  
  9. #include <clib/exec_protos.h>
  10.  
  11. #include "make.h"
  12.  
  13. struct parameters Param = {
  14.     {
  15.     (struct Node *)&Param.filelist.lh_Tail,    /* lh_Head */
  16.     (struct Node *)NULL,                    /* lh_Tail */
  17.     (struct Node *)&Param.filelist.lh_Head,    /* lh_TailPred */
  18.     (UBYTE)NT_USER,
  19.     (UBYTE)0
  20.     },    /* filelist */
  21.     1024, /* MaxLine */
  22.     1,    /* verbosity */
  23.     0,    /* log */
  24.     0,    /* debug */
  25.     0,    /* touch mode */
  26.     0,    /* all mode */
  27.     0,    /* no builtins */
  28.     0,    /* pretend mode */
  29.     0,    /* print database */
  30.     NULL /* makefile */
  31. };
  32.  
  33. const char *usage_string =
  34.     "Usage: make -abdhnpt -v# +m# -fmakefile var=value wildcards...";
  35.  
  36. const char *help_lines[] = {
  37.     "-a\t"    "make all dependents regardless of modification time",
  38.     "-b\t"    "do not use builtin rules",
  39. #if DEBUG
  40.     "-d\t"    "debug mode (enable printf)",
  41. #endif
  42.     "+m#\t"    "sets the maximum line length to # (minimum = 1024)",
  43.     "-n\t"    "prints commands to be executed, but do not execute them",
  44.     "-p\t"    "prints the database, but do not run",
  45.     "-t\t"    "make targets up to date by touching (commands not executed)",
  46.     "-v#\t"    "set verbose level to #; 0=silent",
  47.     "-fmakefile\t"    "specify the makefile to run",
  48.     "var=value\t"    "assigns the value to a variable",
  49.     "wildcards\t"    "specify targets to make",
  50.     NULL
  51. };
  52.  
  53. void
  54. usage( void )
  55. {
  56.     printf( "%s %s\n", version_string+7, verdate_string );
  57.     puts( usage_string );
  58. }
  59.  
  60. int
  61. help( void )
  62. {
  63.     int i = 0;
  64.     char *helpline;
  65.  
  66.     usage();
  67.     puts( copyright_string );
  68.     puts( "" );
  69.     while( helpline = help_lines[ i++ ] ) {
  70.         puts( helpline );
  71.     }
  72.  
  73.     return( 0 );
  74. }
  75.  
  76. static int
  77. expand_wildcard( const char *wild )
  78. {
  79.     struct string_node *sptr = NULL;
  80.     char *fn;
  81.  
  82.     while( fn = scdir( wild )) {
  83.         if( sptr = new_snode( fn )) {
  84.             AddTail( &Param.filelist, &sptr->node );
  85.         }
  86.         else return( 1 );
  87.     }
  88.     return( 0 );
  89. }
  90.  
  91. int
  92. parse_parameters( int argc, char *argv[] )
  93. {
  94.     char *arg;
  95.  
  96.     for( int i = 1; i < argc; i++ ) {
  97.         if( !(arg = argv[ i ] ))
  98.             break;
  99.         switch( *arg ) {
  100.         case '-':
  101.             switch( *++arg ) {
  102.             default:
  103.             case '?':
  104.                 usage();
  105.                 goto death;
  106.             case 'V':
  107.                 printf( "%s %s\n", version_string + 7, verdate_string );
  108.                 puts( copyright_string );
  109.                 goto death;
  110.             case 'a':
  111.                 Param.all_mode = 1;
  112.                 break;
  113.             case 'b':
  114.                 Param.no_builtins = 1;
  115.                 break;
  116.             case 'd':
  117. #if DEBUG
  118.                 Param.debug = 1;
  119. #else
  120.                 puts( "Debugging not available!" );
  121. #endif
  122.                 break;
  123.             case 'f':
  124.                 if( Param.makefile ) {
  125.                     puts( "only one -f argument is allowed" );
  126.                     goto death;
  127.                 }
  128.                 else {
  129.                     if( *++arg )
  130.                         Param.makefile = strdup( arg );
  131.                     else if( ++i < argc ) {
  132.                         arg = argv[ i ];
  133.                         if( *arg )
  134.                             Param.makefile = strdup( arg );
  135.                     }
  136.                 }
  137.                 break;
  138.             case 'h':
  139.                 help();
  140.                 goto death;
  141.             case 'n':
  142.                 Param.pretend_mode = 1;
  143.                 break;
  144.             case 'p':
  145.                 Param.print_database = 1;
  146.                 break;
  147.             case 't':
  148.                 Param.touch_mode = 1;
  149.                 break;
  150.             case 'v':
  151.                 Param.verbosity = atoi( ++arg );
  152.                 break;
  153.             }
  154.             break;
  155.         case '+':
  156.             switch( *++arg ) {
  157.             case 'l':
  158.                 Param.log = (Param.log) ? 0 : 1;
  159.                 break;
  160.             case 'm':
  161.                 {
  162.                     int maxline = atoi( ++arg );
  163.                     Param.MaxLine = max( Param.MaxLine, maxline);
  164.                 }
  165.             }
  166.             break;
  167.         case '?':
  168.             usage();
  169.             goto death;
  170.         default: /* a filename */
  171.             if( scdir( arg ) ) {
  172.                 scdir_abort();
  173.                 if( expand_wildcard( arg ))
  174.                     goto death;
  175.             }
  176.             else {
  177.                 struct string_node *sptr = new_snode( arg );
  178.                 if( sptr ) {
  179.                     AddTail( &Param.filelist, &sptr->node );
  180.                 }
  181.             }
  182.             break;
  183.         }
  184.     }
  185.     if( !Param.makefile )
  186.         Param.makefile = strdup( "Makefile" );
  187.  
  188.     return( 0 );
  189. death:
  190.     return( 1 );
  191. }
  192.  
  193. void
  194. delete_params( void )
  195. {
  196.     if( Param.makefile ) {
  197.         free( Param.makefile );
  198.         Param.makefile = NULL;
  199.     }
  200.     /* free Param */
  201.     delete_slist( &Param.filelist );
  202.     NewList( &Param.filelist );
  203. }
  204.